package cn.dengta.webapp.base.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import cn.dengta.common.util.Common;
import cn.dengta.context.model.AppContext;
import cn.dengta.context.model.WesKit;
import cn.dengta.context.web.ImageConverter;
import me.codeplayer.util.StringUtil;
import org.apache.commons.lang3.StringUtils;

public class ConvertFilterUtil {

	/** flala 需要替换的 oss 域名 替换成  https://oss.flala6.com */
	public static final List<String> COVER_DOMAINS = List.of("https://prod-flala.oss-ap-southeast-1.aliyuncs.com", "https://prod-liaoyu.oss-cn-shenzhen.aliyuncs.com");

	/** 聊郁 需要替换的 oss 域名 替换成  https://oss.guangxijunxuan.cn */
	public static final List<String> COVER_LIAOYU_DOMAINS = List.of("https://prod-flala.oss-ap-southeast-1.aliyuncs.com", ImageConverter.CDN);

	public static int MIN_DOMAIN_LENGTH = 0;
	public static int MIN_LIAOYU_DOMAIN_LENGTH = 0;

	static {
		for (String s : COVER_DOMAINS) {
			if (MIN_DOMAIN_LENGTH == 0) {
				MIN_DOMAIN_LENGTH = s.length();
			}
			if (MIN_DOMAIN_LENGTH > s.length()) {
				MIN_DOMAIN_LENGTH = s.length();
			}
		}
		for (String s : COVER_LIAOYU_DOMAINS) {
			if (MIN_LIAOYU_DOMAIN_LENGTH == 0) {
				MIN_LIAOYU_DOMAIN_LENGTH = s.length();
			}
			if (MIN_LIAOYU_DOMAIN_LENGTH > s.length()) {
				MIN_LIAOYU_DOMAIN_LENGTH = s.length();
			}
		}
	}

	/**
	 * 域名转换
	 */
	public static String convertDomainUrl(String url) {
		if (StringUtil.notEmpty(url) && StringUtils.startsWithAny(url, "http://", "https://")) {
			if (AppContext.inter() && url.length() > MIN_LIAOYU_DOMAIN_LENGTH) {
				for (String domain : COVER_LIAOYU_DOMAINS) {
					url = url.replace(domain, ImageConverter.CDN_INCHAT);
				}
			}
			if (!AppContext.inter() && url.length() > MIN_DOMAIN_LENGTH) {
				for (String domain : COVER_DOMAINS) {
					url = url.replace(domain, ImageConverter.CDN);
				}
			}
		}
		return url;
	}

	/**
	 * 转换默认头像
	 * 支持逗号分隔的多个url
	 */
	public static String convertGenderUrl(WesKit wesKit, String url) {
		if (wesKit != null && StringUtil.notEmpty(url) && StringUtils.startsWithAny(url, "http://", "https://") &&
				StringUtils.endsWithAny(url, "female.png", "male.png")) {
			/**
			 * https://oss.flala6.com/internal/pic/female.png
			 * https://oss.flala6.com/internal/pic/contala_female.png
			 */
			List<String> list = StringUtil.split(url, Common.SEP, String::toString);
			StringBuilder str = new StringBuilder(url.length());
			for (String s : list) {
				final String path = getUrlPath(s);
				final String up = (wesKit.isMain() || wesKit == WesKit.main ? "" : (wesKit.name() + "_"));
				final String fe = "/internal/pic/" + up + "female.png";
				if (StringUtils.endsWith(s, "female.png")) {
					str.append(StringUtils.replace(s, path, fe));
					continue;
				}
				final String ma = "/internal/pic/" + up + "male.png";
				if (StringUtils.endsWith(s, "male.png")) {
					str.append(StringUtils.replace(s, path, ma));
				}
			}
			return str.toString();
		}
		return url;
	}

	/** 获取路径 */
	private static String getUrlPath(String urlStr) {
		try {
			return new URL(urlStr).getPath();
		} catch (MalformedURLException e) {
		}
		return null;
	}

}
